package com.teamcenter.rac.massupdate.handlers;

import java.util.Map;

import org.apache.log4j.Logger;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.IHandlerService;

import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.massupdate.Activator;

public abstract class AbstractMassUpdateHandler
  extends AbstractHandler
{
  protected static final String ADD_TO_MARKUP_PARAM = "com.teamcenter.rac.massupdate.addToMarkup";
  protected static final String CHANGE_OBJECT_PARAM = "com.teamcenter.rac.massupdate.changeObject";
  protected static final String MARKUP_PARAM = "com.teamcenter.rac.massupdate.markup";
  protected static final String OPEN_MANAGE_MARKUP_COMMAND_ID = "com.teamcenter.rac.massupdate.openManageMarkup";
  protected static final String OPEN_MARKUP_RESULTS_COMMAND_ID = "com.teamcenter.rac.massupdate.openMarkupResults";
  protected static final String OPEN_RESULTS_COMMAND_ID = "com.teamcenter.rac.massupdate.openResults";
  protected static final String OPEN_WIZARD_COMMAND_ID = "com.teamcenter.rac.massupdate.openWizard";
  protected static final String REPLACEMENT_PART_PARAM = "com.teamcenter.rac.massupdate.replacementPart";
  protected static final String TARGET_PART_PARAM = "com.teamcenter.rac.massupdate.targetPart";
  private TCSession m_tcSession;
  
  protected void callHandler(String paramString, Map<String, String> paramMap)
  {
    ICommandService localICommandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
    IHandlerService localIHandlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
    final Command localCommand = localICommandService.getCommand(paramString);
    ParameterizedCommand localParameterizedCommand = ParameterizedCommand.generateCommand(localCommand, paramMap);
    final ExecutionEvent localExecutionEvent = localIHandlerService.createExecutionEvent(localParameterizedCommand, null);
    if (Display.getCurrent() != null) {
      try
      {
        localCommand.executeWithChecks(localExecutionEvent);
      }
      catch (Exception localException)
      {
        Logger.getLogger(AbstractMassUpdateHandler.class).error(localException.getLocalizedMessage(), localException);
      }
    } else {
      Display.getDefault().asyncExec(new Runnable()
      {
        public void run()
        {
          try
          {
            localCommand.executeWithChecks(localExecutionEvent);
          }
          catch (Exception localException)
          {
            Logger.getLogger(AbstractMassUpdateHandler.class).error(localException.getLocalizedMessage(), localException);
          }
        }
      });
    }
  }
  
  protected TCSession getTCSession()
  {
    try
    {
      if (this.m_tcSession == null) {
        this.m_tcSession = ((TCSession)Activator.getDefault().getSessionService().getSession(TCSession.class.getName()));
      }
    }
    catch (Exception localException)
    {
      Logger.getLogger(AbstractMassUpdateHandler.class).error(localException.getLocalizedMessage(), localException);
    }
    return this.m_tcSession;
  }
}

